home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / Services / Weather / Globalweather.php < prev    next >
Encoding:
PHP Script  |  2004-10-01  |  16.2 KB  |  417 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2004 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available through the world-wide-web at                              |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Alexander Wirtz <alex@pc4p.net>                             |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Globalweather.php,v 1.27 2004/05/04 13:57:34 eru Exp $
  20.  
  21. /**
  22. * @package      Services_Weather
  23. * @filesource
  24. */
  25.  
  26. /**
  27. */
  28. require_once "Services/Weather/Common.php";
  29.  
  30. // {{{ class Services_Weather_Globalweather
  31. /**
  32. * PEAR::Services_Weather_Globalweather
  33. *
  34. * This class acts as an interface to the soap service of capescience.com.
  35. * It searches for given locations and retrieves current weather data.
  36. *
  37. * GlobalWeather is a SOAP frontend for METAR data, provided by CapeScience.
  38. * If you want to use METAR, you should try this class first, as it is much
  39. * more comfortable (and also a bit faster) than the native METAR-class
  40. * provided by this package. On the other hand, this service won't supply
  41. * TAFs, the forecast system accompanying METAR, so you have to make
  42. * the call here...
  43. *
  44. * For a working example, please take a look at
  45. *     docs/Services_Weather/examples/globalweather-basic.php
  46. *
  47. * @author       Alexander Wirtz <alex@pc4p.net>
  48. * @link         http://www.capescience.com/webservices/globalweather/index.shtml
  49. * @example      examples/globalweather-basic.php globalweather-basic.php
  50. * @package      Services_Weather
  51. * @license      http://www.php.net/license/2_02.txt
  52. * @version      1.3
  53. */
  54. class Services_Weather_Globalweather extends Services_Weather_Common {
  55.  
  56.     // {{{ properties
  57.     /**
  58.     * WSDL object, provided by CapeScience
  59.     *
  60.     * @var      object                      $_wsdl
  61.     * @access   private
  62.     */
  63.     var $_wsdl;
  64.  
  65.     /**
  66.     * SOAP object to access station data, provided by CapeScience
  67.     *
  68.     * @var      object                      $_stationSoap
  69.     * @access   private
  70.     */
  71.     var $_stationSoap;
  72.  
  73.     /**
  74.     * SOAP object to access weather data, provided by CapeScience
  75.     *
  76.     * @var      object                      $_weaterSoap
  77.     * @access   private
  78.     */
  79.     var $_weatherSoap;
  80.     // }}}
  81.  
  82.     // {{{ constructor
  83.     /**
  84.     * Constructor
  85.     *
  86.     * Requires SOAP to be installed
  87.     *
  88.     * @param    array                       $options
  89.     * @param    mixed                       $error
  90.     * @throws   PEAR_Error
  91.     * @throws   PEAR_Error::SERVICES_WEATHER_ERROR_WRONG_SERVER_DATA
  92.     * @see      Science_Weather::Science_Weather
  93.     * @access   private
  94.     */
  95.     function Services_Weather_Globalweather($options, &$error)
  96.     {
  97.         $perror = null;
  98.         $this->Services_Weather_Common($options, $perror);
  99.         if (Services_Weather::isError($perror)) {
  100.             $error = $perror;
  101.             return;
  102.         }
  103.  
  104.         include_once "SOAP/Client.php";
  105.         $this->_wsdl = new SOAP_WSDL("http://live.capescience.com/wsdl/GlobalWeather.wsdl", array("timeout" => $this->_httpTimeout));
  106.         if (isset($this->_wsdl->fault) && Services_Weather::isError($this->_wsdl->fault)) {
  107.             $error = Services_Weather::raiseError(SERVICES_WEATHER_ERROR_WRONG_SERVER_DATA, __FILE__, __LINE__);
  108.             return;
  109.         }
  110.  
  111.         eval($this->_wsdl->generateAllProxies());
  112.         if (!class_exists("WebService_GlobalWeather_StationInfo") || !class_exists("WebService_GlobalWeather_GlobalWeather")) {
  113.             $error = Services_Weather::raiseError(SERVICES_WEATHER_ERROR_WRONG_SERVER_DATA, __FILE__, __LINE__);
  114.             return;
  115.         }
  116.  
  117.         $this->_stationSoap = &new WebService_GlobalWeather_StationInfo;
  118.         $this->_weatherSoap = &new WebService_GlobalWeather_GlobalWeather;
  119.     }
  120.     // }}}
  121.  
  122.     // {{{ _checkLocationID()
  123.     /**
  124.     * Checks the id for valid values and thus prevents silly requests to
  125.     * GlobalWeather server
  126.     *
  127.     * @param    string                      $id
  128.     * @return   PEAR_Error|bool
  129.     * @throws   PEAR_Error::SERVICES_WEATHER_ERROR_NO_LOCATION
  130.     * @throws   PEAR_Error::SERVICES_WEATHER_ERROR_INVALID_LOCATION
  131.     * @access   private
  132.     */
  133.     function _checkLocationID($id)
  134.     {
  135.         if (is_array($id) || is_object($id) || !strlen($id)) {
  136.             return Services_Weather::raiseError(SERVICES_WEATHER_ERROR_NO_LOCATION, __FILE__, __LINE__);
  137.         } elseif ($this->_stationSoap->isValidCode($id) === false) {
  138.             return Services_Weather::raiseError(SERVICES_WEATHER_ERROR_INVALID_LOCATION, __FILE__, __LINE__);
  139.         }
  140.  
  141.         return true;
  142.     }
  143.     // }}}
  144.  
  145.     // {{{ searchLocation()
  146.     /**
  147.     * Searches IDs for given location, returns array of possible locations
  148.     * or single ID
  149.     *
  150.     * @param    string                      $location
  151.     * @param    bool                        $useFirst       If set, first ID of result-array is returned
  152.     * @return   PEAR_Error|array|string
  153.     * @throws   PEAR_Error::SERVICES_WEATHER_ERROR_WRONG_SERVER_DATA
  154.     * @throws   PEAR_Error::SERVICES_WEATHER_ERROR_UNKNOWN_LOCATION
  155.     * @access   public
  156.     */
  157.     function searchLocation($location, $useFirst = false)
  158.     {
  159.         // Get search data from server and unserialize
  160.         $search = $this->_stationSoap->searchByName($location);
  161.  
  162.         if (Services_Weather::isError($search)) {
  163.             return Services_Weather::raiseError(SERVICES_WEATHER_ERROR_WRONG_SERVER_DATA, __FILE__, __LINE__);
  164.         } else {
  165.             if (!is_array($search) || !sizeof($search)) {
  166.                 return Services_Weather::raiseError(SERVICES_WEATHER_ERROR_UNKNOWN_LOCATION, __FILE__, __LINE__);
  167.             } else {
  168.                 if (!$useFirst && (sizeof($search) > 1)) {
  169.                     $searchReturn = array();
  170.                     for ($i = 0; $i < sizeof($search); $i++) {
  171.                         $searchReturn[$search[$i]->icao] = $search[$i]->name.", ".$search[$i]->country;
  172.                     }
  173.                 } elseif ($useFirst || (sizeof($search) == 1)) {
  174.                     $searchReturn = $search[0]->icao;
  175.                 }
  176.             }
  177.         }
  178.  
  179.         return $searchReturn;
  180.     }
  181.     // }}}
  182.  
  183.     // {{{ searchLocationByCountry()
  184.     /**
  185.     * Returns IDs with location-name for a given country or all available
  186.     * countries, if no value was given 
  187.     *
  188.     * @param    string                      $country
  189.     * @return   PEAR_Error|array
  190.     * @throws   PEAR_Error::SERVICES_WEATHER_ERROR_WRONG_SERVER_DATA
  191.     * @throws   PEAR_Error::SERVICES_WEATHER_ERROR_UNKNOWN_LOCATION
  192.     * @access   public
  193.     */
  194.     function searchLocationByCountry($country = "")
  195.     {
  196.         // Return the available countries as no country was given
  197.         if (!strlen($country)) {
  198.             $countries = $this->_stationSoap->listCountries();
  199.             if (Services_Weather::isError($countries)) {
  200.                 return Services_Weather::raiseError(SERVICES_WEATHER_ERROR_WRONG_SERVER_DATA, __FILE__, __LINE__);
  201.             }
  202.             return $countries;
  203.         }
  204.  
  205.         // Now for the real search
  206.         $countryLocs = $this->_stationSoap->searchByCountry($country);
  207.         // Check result for validity
  208.         if (Services_Weather::isError($countryLocs)) {
  209.             return Services_Weather::raiseError(SERVICES_WEATHER_ERROR_WRONG_SERVER_DATA, __FILE__, __LINE__);
  210.         } elseif (!is_array($countryLocs)) {
  211.             return Services_Weather::raiseError(SERVICES_WEATHER_ERROR_UNKNOWN_LOCATION, __FILE__, __LINE__);
  212.         }
  213.  
  214.         // Construct the result
  215.         $locations = array();
  216.         foreach ($countryLocs as $location) {
  217.             $locations[$location->icao] = $location->name.", ".$location->country;
  218.         }
  219.         asort($locations);
  220.  
  221.         return $locations;
  222.     }
  223.     // }}}
  224.  
  225.     // {{{ getLocation()
  226.     /**
  227.     * Returns the data for the location belonging to the ID
  228.     *
  229.     * @param    string                      $id
  230.     * @return   PEAR_Error|array
  231.     * @throws   PEAR_Error
  232.     * @access   public
  233.     */
  234.     function getLocation($id = "")
  235.     {
  236.         $status = $this->_checkLocationID($id);
  237.  
  238.         if (Services_Weather::isError($status)) {
  239.             return $status;
  240.         }
  241.  
  242.         $locationReturn = array();
  243.  
  244.         if ($this->_cacheEnabled && ($location = $this->_cache->get("GW-".$id, "location"))) {
  245.             // Get data from cache
  246.             $this->_location = $location;
  247.             $locationReturn["cache"] = "HIT";
  248.         } else {
  249.             $location = $this->_stationSoap->getStation($id);
  250.  
  251.             if (Services_Weather::isError($location)) {
  252.                 return $location;
  253.             }
  254.  
  255.             $this->_location = $location;
  256.  
  257.             if ($this->_cacheEnabled) {
  258.                 // ...and cache it
  259.                 $expire = constant("SERVICES_WEATHER_EXPIRES_LOCATION");
  260.                 $this->_cache->extSave("GW-".$id, $this->_location, "", $expire, "location");
  261.             }
  262.             $locationReturn["cache"] = "MISS";
  263.         }
  264.         if (strlen($this->_location->region) && strlen($this->_location->country)) {
  265.             $locname = $this->_location->name.", ".$this->_location->region.", ".$this->_location->country;
  266.         } elseif (strlen($this->_location->country)) {
  267.             $locname = $this->_location->name.", ".$this->_location->country;
  268.         } else {
  269.             $locname = $this->_location->name;
  270.         }
  271.         $locationReturn["name"]      = $locname;
  272.         $locationReturn["latitude"]  = $this->_location->latitude;
  273.         $locationReturn["longitude"] = $this->_location->longitude;
  274.         $locationReturn["elevation"] = $this->_location->elevation;
  275.  
  276.         return $locationReturn;
  277.     }
  278.     // }}}
  279.  
  280.     // {{{ getWeather()
  281.     /**
  282.     * Returns the weather-data for the supplied location
  283.     *
  284.     * @param    string                      $id
  285.     * @param    string                      $unitsFormat
  286.     * @return   PEAR_Error|array
  287.     * @throws   PEAR_Error
  288.     * @access   public
  289.     */
  290.     function getWeather($id = "", $unitsFormat = "")
  291.     {
  292.         static $clouds;
  293.         if (!isset($clouds)) {
  294.             $clouds    = array(
  295.                 "sky clear",
  296.                 "few",
  297.                 "scattered",
  298.                 "broken",
  299.                 "overcast",
  300.             );
  301.         }
  302.         
  303.         $status = $this->_checkLocationID($id);
  304.  
  305.         if (Services_Weather::isError($status)) {
  306.             return $status;
  307.         }
  308.  
  309.         // Get other data
  310.         $units    = $this->getUnitsFormat($unitsFormat);
  311.  
  312.         $weatherReturn = array();
  313.         if ($this->_cacheEnabled && ($weather = $this->_cache->get("GW-".$id, "weather"))) {
  314.             // Same procedure...
  315.             $this->_weather = $weather;
  316.             $weatherReturn["cache"] = "HIT";
  317.         } else {
  318.             // ...as last function
  319.             $weather = $this->_weatherSoap->getWeatherReport($id);
  320.  
  321.             if (Services_Weather::isError($weather)) {
  322.                 return $weather;
  323.             }
  324.  
  325.             $this->_weather = $weather;
  326.  
  327.             if ($this->_cacheEnabled) {
  328.                 // ...and cache it
  329.                 $expire = constant("SERVICES_WEATHER_EXPIRES_WEATHER");
  330.                 $this->_cache->extSave("GW-".$id, $this->_weather, "", $expire, "weather");
  331.             }
  332.             $weatherReturn["cache"] = "MISS";
  333.         }
  334.  
  335.         $update = trim(str_replace(array("T", "Z"), " ", $this->_weather->timestamp))." GMT";
  336.  
  337.         $weatherReturn["update"]            = gmdate(trim($this->_dateFormat." ".$this->_timeFormat), strtotime($update));
  338.         $weatherReturn["updateRaw"]         = $this->_weather->timestamp;
  339.         if (strlen($this->_weather->station->region) && strlen($this->_weather->station->country)) {
  340.             $locname = $this->_weather->station->name.", ".$this->_weather->station->region.", ".$this->_weather->station->country;
  341.         } elseif (strlen($this->_weather->station->country)) {
  342.             $locname = $this->_weather->station->name.", ".$this->_weather->station->country;
  343.         } else {
  344.             $locname = $this->_weather->station->name;
  345.         }
  346.         $weatherReturn["station"]           = $locname;
  347.         $weatherReturn["wind"]              = $this->convertSpeed($this->_weather->wind->prevailing_speed, "mps", $units["wind"]);
  348.         $weatherReturn["windDegrees"]       = $this->_weather->wind->prevailing_direction->degrees;
  349.         $weatherReturn["windDirection"]     = $this->_weather->wind->prevailing_direction->compass;
  350.         if ($this->_weather->wind->prevailing_speed != $this->_weather->wind->gust_speed) {
  351.             $weatherReturn["windGust"]      = $this->convertSpeed($this->_weather->wind->gust_speed, "mps", $units["wind"]);
  352.         }
  353.         if ($this->_weather->wind->varying_from_direction != "" && $this->_weather->wind->varying_to_direction != "") {
  354.             $weatherReturn["windVar"]       = array (
  355.                 "from" => $this->_weather->wind->varying_from_direction,
  356.                 "to"   => $this->_weather->wind->varying_to_direction
  357.             );
  358.         }
  359.  
  360.         $weatherReturn["visibility"]        = $this->convertDistance($this->_weather->visibility->distance / 1000, "km", $units["vis"]);
  361.         $weatherReturn["visQualifier"]      = $this->_weather->visibility->qualifier;
  362.  
  363.         $condition = array();
  364.         for ($i = 0; $i < sizeof($this->_weather->phenomena); $i++) {
  365.             $condition[] = $this->_weather->phenomena[$i]->string;
  366.         }
  367.         $weatherReturn["condition"]         = implode(", ", $condition);
  368.  
  369.         if (is_array($this->_weather->sky->layers)) {
  370.             $layers = array();
  371.             for ($i = 0; $i < sizeof($this->_weather->sky->layers); $i++) {
  372.                 if (strtoupper($this->_weather->sky->layers[$i]->type) != "CLEAR") {
  373.                     $layers[$i]             = array();
  374.                     $layers[$i]["amount"]   = $clouds[$this->_weather->sky->layers[$i]->extent];
  375.                     $layers[$i]["height"]   = $this->convertDistance($this->_weather->sky->layers[$i]->altitude, "m", $units["height"]);
  376.                     if (strtoupper($this->_weather->sky->layers[$i]->type) != "CLOUD") {
  377.                         $layers[$i]["type"] = ucwords(str_replace("_", "", $this->_weather->sky->layers[$i]->type));
  378.                     }
  379.                 }
  380.             }
  381.             if (sizeof($layers)) {
  382.                 $weatherReturn["clouds"]        = $layers;
  383.             }
  384.         }
  385.  
  386.         $weatherReturn["temperature"]       = $this->convertTemperature($this->_weather->temperature->ambient, "c", $units["temp"]);
  387.         $feltTemperature = $this->calculateWindChill($this->convertTemperature($weatherReturn["temperature"], $units["temp"], "f"), $this->convertSpeed($weatherReturn["wind"], $units["wind"], "mph"));
  388.         $weatherReturn["feltTemperature"]   = $this->convertTemperature($feltTemperature, "f", $units["temp"]);
  389.         $weatherReturn["dewPoint"]          = $this->convertTemperature($this->_weather->temperature->dewpoint, "c", $units["temp"]);
  390.         $weatherReturn["humidity"]          = $this->_weather->temperature->relative_humidity;
  391.         $weatherReturn["pressure"]          = $this->convertPressure($this->_weather->pressure->altimeter, "hpa", $units["pres"]);
  392.  
  393.         return $weatherReturn;
  394.     }
  395.     // }}}
  396.  
  397.     // {{{ getForecast()
  398.     /**
  399.     * GlobalWeather has no forecast per se, so this function is just for
  400.     * compatibility purposes.
  401.     *
  402.     * @param    string                      $int
  403.     * @param    int                         $days
  404.     * @param    string                      $unitsFormat
  405.     * @return   bool
  406.     * @access   public
  407.     * @deprecated
  408.     */
  409.     function getForecast($id = null, $days = null, $unitsFormat = null)
  410.     {
  411.         return false;
  412.     }
  413.     // }}}
  414. }
  415. // }}}
  416. ?>
  417.